Imports System.Linq
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.DataAcessLayer.PersistentStorage

Namespace Services
    Public Class EmployeeService
        Inherits ServiceBase
        Implements IEmployeeService

        Public Function Create(employeeCode As String, firstName As String, middleName As String, lastName As String, homeAddress As String, contactNumber As String, emailAddress As String, courseId As Integer, employeeTypeId As Integer, year As Integer, createdBy As Integer) As Integer Implements IEmployeeService.Create
            StatementSource = _
                New CreateEmployeeStatementSource(employeeCode, firstName, middleName, lastName, homeAddress, contactNumber, emailAddress, courseId, employeeTypeId, year, createdBy)
            Return DbHelper.ExecuteNonQuery(StatementSource)
        End Function

        Public Sub Delete(employeeId As Integer) Implements IEmployeeService.Delete
            StatementSource = New DeleteEmployeeStatementSource(employeeId)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Edit(entity As Employee, modifiedBy As Integer) Implements IEmployeeService.Edit
            StatementSource = _
                New UpdateEmployeeStatementSource(entity.EmployeeId, entity.FirstName, entity.MiddleName, entity.LastName, entity.HomeAddress, entity.ContactNumber, entity.EmailAddress, entity.Course.CourseId, entity.EmployeeType.EmployeeTypeId, entity.EmployeeCode, entity.Year, modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAll() As IEnumerable(Of Employee) Implements IEmployeeService.ReadAll
            StatementSource = New ReadAllEmployeeStatementSource()
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadAllByScheduleId(scheduleId As Integer) As IEnumerable(Of Employee) Implements IEmployeeService.ReadAllByScheduleId
            StatementSource = New ReadAllEmployeeByScheduleIdStatementSource(scheduleId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadById(employeeId As Integer) As Employee Implements IEmployeeService.ReadById
            StatementSource = New ReadByEmployeeIdStatementSource(employeeId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Public Function ReadByCode(employeeCode As String) As Employee Implements IEmployeeService.ReadByCode
            StatementSource = New ReadByEmployeeCodeStatementSource(employeeCode)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Public Function ReadForEmployeeCode(courseId As Integer, schoolYear As Integer) As Integer Implements IEmployeeService.ReadForEmployeeCode
            StatementSource = New ReadEmployeeForCodeStatementSource(courseId, schoolYear)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Dim count As Integer = 0
            If dataReader.HasRows Then
                While dataReader.Read()
                    count = Convert.ToInt32(dataReader("EmployeeCount"))
                End While
            End If

            Return count
        End Function

        Private Function MapDataToEntity(dataReader As SqlDataReader) As IEnumerable(Of Employee)
            Dim entities As New List(Of Employee)

            If (dataReader.HasRows) Then
                Dim courseService As New CourseService
                Dim employeeTypeService As New EmployeeTypeService

                While (dataReader.Read())
                    Dim entity As New Employee()
                    Dim courseId As Integer = Convert.ToInt32(dataReader("CourseId"))
                    Dim typeId As Integer = Convert.ToInt32(dataReader("EmployeeTypeId"))

                    entity.EmployeeCode = Convert.ToString(dataReader("EmployeeCode"))
                    entity.ContactNumber = Convert.ToString(dataReader("ContactNumber"))
                    entity.Course = courseService.ReadById(courseId)
                    entity.EmailAddress = Convert.ToString(dataReader("EmailAddress"))
                    entity.FirstName = Convert.ToString(dataReader("FirstName"))
                    entity.HomeAddress = Convert.ToString(dataReader("HomeAddress"))
                    entity.EmployeeId = Convert.ToInt32(dataReader("EmployeeId"))
                    entity.IsActive = Convert.ToBoolean(dataReader("IsActive"))
                    entity.LastName = Convert.ToString(dataReader("LastName"))
                    entity.MiddleName = Convert.ToString(dataReader("MiddleName"))
                    entity.EmployeeType = employeeTypeService.ReadById(typeId)
                    entity.Year = Convert.ToInt32(dataReader("Year"))

                    entities.Add(entity)
                End While
            End If

            Return entities
        End Function

    End Class
End Namespace